﻿using EmitMapper;
using STHPMS1.Common.C_Entity;
using STHPMS1.Common.C_Entity.Stmpms1;
using STHPMS1.ICore.I_DAL;
using STHPMS1.ICore.I_Entity.Stmpms1;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace STHPMS1.BLL.B_Serv
{
    public class Stake
    {
        public double BeginMp;
        public double EndMp;
    }
    public class RoadSaveService
    {
        public List<B_Invntory> AllInvntoryList=new List<B_Invntory>();//全幅路段
        public List<B_Unit> AllUnitList = new List<B_Unit>();//全幅单元
        public List<B_Invntory_Unit> AllRelList = new List<B_Invntory_Unit>();//全幅关联


        public List<B_Invntory> UpInvntoryList = new List<B_Invntory>();//上行路段
        public List<B_Unit> UpUnitList = new List<B_Unit>();//上行单元
        public List<B_Invntory_Unit> UpRelList = new List<B_Invntory_Unit>();//上行关联


        public List<B_Invntory> DownInvntoryList = new List<B_Invntory>();//下行路段
        public List<B_Unit> DownUnitList = new List<B_Unit>();//下行单元
        public List<B_Invntory_Unit> DownRelList = new List<B_Invntory_Unit>();//下行关联



        private List<B_Rdstandard_History> _stdHis;
        private List<B_Sturcture_History> _stuHis;
        private List<B_Pmstc_History> _pmsHis;
        private List<B_Layer_History> _layHis;



        List<B_Invntory_History> invHis = new List<B_Invntory_History>();
        List<B_Unit_History> unitHis = new List<B_Unit_History>();
        List<B_Invntory_Unit_History> relHis = new List<B_Invntory_Unit_History>();



        public void SyncAllStake(ISthpms1Dao dao, string rdwayid, double version, List<Stake> stakeList)
        {
            List<string> upSegIdList = new List<string>();
            foreach (var item in UpInvntoryList)
            {
                if (stakeList.Exists(m => item.Beginmp >= m.BeginMp && item.Endmp <= m.EndMp))
                {
                    upSegIdList.Add(item.Segid);
                    UpUnitList.RemoveAll(m => m.Beginmp >= item.Beginmp && m.Endmp <= item.Endmp);
                }
            }

            List<string> downSegIdList = new List<string>();
            foreach (var item in DownInvntoryList)
            {
                if (stakeList.Exists(m => item.Beginmp >= m.BeginMp && item.Endmp <= m.EndMp))
                {
                    downSegIdList.Add(item.Segid);
                    DownUnitList.RemoveAll(m => m.Beginmp >= item.Beginmp && m.Endmp <= item.Endmp);
                }
            }

            foreach (var segid in upSegIdList)
            {
                UpInvntoryList.RemoveAll(m => m.Segid == segid);
                UpRelList.RemoveAll(m => m.Segid == segid);
            }

            foreach (var segid in downSegIdList)
            {
                DownInvntoryList.RemoveAll(m => m.Segid == segid);
                DownRelList.RemoveAll(m => m.Segid == segid);
            }

            foreach (var item in stakeList)
            {
                SegDividedService serv = new SegDividedService();
                serv.GenerateRoadSegAndUnit(dao, rdwayid, version,item.BeginMp,item.EndMp);
                serv.SaveData(this);
                serv.Dispose();
            }
        }

        public bool ValidateData(ISthpms1Dao dao, string rdwayid)
        {
            if (dao == null) throw new Exception("数据库操作对象为空!");
            var road = dao.Single<B_Roadway>(p => p.Rdwayid == rdwayid);
            if (dao.B_Invntory.GetNewVersion(rdwayid) <= road.His_Version) return false;

            var standardList = dao.Select<B_Rdstandard>(p => p.Rdwayid == rdwayid , new DefPageInfo(), k => k.Beginmp);
            var _sturctureList = dao.Select<B_Sturcture>(p => p.Rdwayid == rdwayid, new DefPageInfo(), k => k.Beginmp);
            var _pmstcList = dao.Select<B_Pmstc>(p => p.Rdwayid == rdwayid, new DefPageInfo(), k => k.Beginmp);
            var _layerList = dao.Select<B_Layer>(p => p.Rdwayid == rdwayid);
          
            foreach (var item in dao.Select<B_Invntory>(p => p.Rdwayid == rdwayid))
            {
                B_Invntory_History history = ObjectMapperManager.DefaultInstance.GetMapper<IB_Invntory, B_Invntory_History>().Map(item);
                if (history != null)
                {
                    history.Importdate = DateTime.Now;
                    invHis.Add(history);
                }
            }
            foreach (var item in dao.Select<B_Unit>(p => p.Rdwayid == rdwayid))
            {
                B_Unit_History history = ObjectMapperManager.DefaultInstance.GetMapper<IB_Unit, B_Unit_History>().Map(item);
                if (history != null)
                {
                    unitHis.Add(history);
                }
            }
            foreach (var item in dao.Select<B_Invntory_Unit>(p => p.Rdwayid == rdwayid))
            {
                B_Invntory_Unit_History history = ObjectMapperManager.DefaultInstance.GetMapper<IB_Invntory_Unit, B_Invntory_Unit_History>().Map(item);
                if (history != null)
                {
                    relHis.Add(history);
                }
            }

            _stdHis = new List<B_Rdstandard_History>();
            foreach (var item in standardList.Where(p => (p.His_Version ?? 0) > (road.His_Version ?? 0)))
            {
                B_Rdstandard_History history = ObjectMapperManager.DefaultInstance.GetMapper<IB_Rdstandard, B_Rdstandard_History>().Map(item);
                if (history != null)
                {
                    history.Importdate = DateTime.Now;
                    _stdHis.Add(history);
                }
            }
            _stuHis = new List<B_Sturcture_History>();
            foreach (var item in _sturctureList.Where(p => (p.His_Version ?? 0) > (road.His_Version ?? 0)))
            {
                B_Sturcture_History history = ObjectMapperManager.DefaultInstance.GetMapper<IB_Sturcture, B_Sturcture_History>().Map(item);
                if (history != null)
                {
                    history.Importdate = DateTime.Now;
                    _stuHis.Add(history);
                }
            }
            _pmsHis = new List<B_Pmstc_History>();
            foreach (var item in _pmstcList.Where(p => (p.His_Version ?? 0) > (road.His_Version ?? 0)))
            {
                B_Pmstc_History history = ObjectMapperManager.DefaultInstance.GetMapper<IB_Pmstc, B_Pmstc_History>().Map(item);
                if (history != null)
                {
                    history.Importdate = DateTime.Now;
                    _pmsHis.Add(history);
                }
            }

            _layHis = new List<B_Layer_History>();
            foreach (var item in _layerList.Where(p => (p.His_Version ?? 0) > (road.His_Version ?? 0)))
            {
                B_Layer_History history = ObjectMapperManager.DefaultInstance.GetMapper<IB_Layer, B_Layer_History>().Map(item);
                if (history != null)
                {
                    history.Importdate = DateTime.Now;
                    _layHis.Add(history);
                }
            }
            if (_stdHis.Count == 0 && _stuHis.Count == 0 && _pmsHis.Count == 0 && _layHis.Count == 0) return false;

            if (standardList.Count == 0) throw new Exception(string.Format("路段划分标准数据不存在！"));
            //if (_sturctureList.Count == 0) throw new Exception(string.Format("该路线下的构造物数据不存在！",Direct.GetEnumDescription()));
            if (_pmstcList.Count == 0) throw new Exception(string.Format("路面结构数据不存在！"));
            if (_layerList.Count == 0) throw new Exception(string.Format("路面结构层数据不存在！"));
            return true;
        }



        public void SaveData(ISthpms1Dao dao, string rdwayid, double version)
        {
            dao.Delete<B_Invntory>(p => p.Rdwayid == rdwayid);
            dao.Delete<B_Unit>(p => p.Rdwayid == rdwayid);
            dao.Delete<B_Invntory_Unit>(p => p.Rdwayid == rdwayid);

            if (AllInvntoryList.Count > 0) dao.SaveAll(AllInvntoryList);
            if (AllUnitList.Count > 0) dao.SaveAll(AllUnitList);
            if (AllRelList.Count > 0) dao.SaveAll(AllRelList);

            if (UpInvntoryList.Count > 0) dao.SaveAll(UpInvntoryList);
            if (UpUnitList.Count > 0) dao.SaveAll(UpUnitList);
            if (UpRelList.Count > 0) dao.SaveAll(UpRelList);

            if (DownInvntoryList.Count > 0) dao.SaveAll(DownInvntoryList);
            if (DownUnitList.Count > 0) dao.SaveAll(DownUnitList);
            if (DownRelList.Count > 0) dao.SaveAll(DownRelList);


            if (_stdHis.Count > 0) dao.SaveAll(_stdHis);
            if (_stuHis.Count > 0) dao.SaveAll(_stuHis);
            if (_pmsHis.Count > 0) dao.SaveAll(_pmsHis);
            if (_layHis.Count > 0) dao.SaveAll(_layHis);

            if (invHis.Count > 0) dao.SaveAll(invHis);
            if (unitHis.Count > 0) dao.SaveAll(unitHis);
            if (relHis.Count > 0) dao.SaveAll(relHis);


            dao.Update<B_Roadway>(new { His_Version = version }, p => p.Rdwayid == rdwayid);

        }

        public void Dispose()
        {
            AllInvntoryList = null;
            AllRelList= null;
            AllUnitList= null;
            UpInvntoryList= null;
            UpRelList = null;
            UpUnitList = null;
            DownInvntoryList = null;
            DownRelList= null;
            DownUnitList = null;
            _stdHis = null;
            _stuHis = null;
            _pmsHis = null;
            _layHis = null;

            invHis = null;
            unitHis = null;
            relHis = null;
        }

    }
}
